package com.fancon.android.gardenjokesx.fragment;

import java.util.ArrayList;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.util.Log;

import com.fancon.android.gardenjokesx.adapter.JokesAdapter;
import com.fancon.android.gardenjokesx.entity.Photo;
import com.fancon.android.gardenjokesx.widget.GardenControlView;
import com.fancon.android.gardenjokesx.widget.IGlobalStorage;
import com.fancon.android.gardenjokesx.widget.IUpdate;

public class GardenJokesBaseFragment extends Fragment implements IUpdate {
	protected GardenControlView topControl;
	protected ArrayList<Photo> listPhotos = new ArrayList<Photo>();
	protected ArrayList<Photo> firstPage = new ArrayList<Photo>();
	protected int index = 1;
	protected Boolean isFirst = true;
	protected boolean loadingMore = false;
	protected Boolean isEnd = false;
	protected JokesAdapter adapter = null;

	public GardenJokesBaseFragment() {
	}

	public GardenControlView getTopControl() {
		return topControl;
	}

	public void setTopControl(GardenControlView topControl) {
		this.topControl = topControl;
	}

	public void onPause() {
		super.onPause();
		// Log.d("pause fragment", "pause fragment"+getClass().getName());
		// listPhotos.clear();
		// listPhotos.addAll(firstPage);
		// //
		// index = 1;
		// isFirst = true;
		// loadingMore = false;
		// isEnd = false;
	}

	@Override
	public void onDestroyView() {
		Log.d("destroy", "destroy view" + getClass().getName());
		super.onDestroyView();
	}

	@Override
	public void doUpdate() {
		// TODO Auto-generated method stub
		try {
			if (((IGlobalStorage) (((Activity) (getActivity()))
					.getApplication())).isChange()&&!isFirst) {
				int position = ((IGlobalStorage) (((Activity) (getActivity()))
						.getApplication())).getCurrentPosition();
				int numberClick = ((IGlobalStorage) (((Activity) (getActivity()))
						.getApplication())).getCurrentPhoto().getNumber_click();
				int numberComment = ((IGlobalStorage) (((Activity) (getActivity()))
						.getApplication())).getCurrentPhoto()
						.getNumber_comment();
				Boolean isLike = ((IGlobalStorage) (((Activity) (getActivity()))
						.getApplication())).getCurrentPhoto()
						.isIs_clicked();
				adapter.getLisPhoto().get(position)
						.setNumber_click(numberClick);
				adapter.getLisPhoto().get(position)
						.setNumber_comment(numberComment);
				adapter.getLisPhoto().get(position)
				.setIs_clicked(isLike);
				adapter.notifyDataSetChanged();
			}
			
			((IGlobalStorage) (((Activity) (getActivity()))
					.getApplication())).setChange(false);
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}
}
